package com.bmd.backgroundmanger.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Value("${file.upload-path}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 配置头像静态资源映射
        String avatarPath = "file:" + uploadPath + "avatar/";
        System.out.println("Avatar physical path: " + avatarPath);
        
        registry.addResourceHandler("/api/avatar/**")
                .addResourceLocations(avatarPath);
        
        registry.addResourceHandler("/avatar/**")
                .addResourceLocations(avatarPath);
        
        // 配置图标静态资源映射
        String iconPath = "file:" + uploadPath + "icons/";
        System.out.println("Icon physical path: " + iconPath);
        
        registry.addResourceHandler("/uploads/icons/**")
                .addResourceLocations(iconPath);
        
        System.out.println("Static resource mapping: /avatar/** -> " + avatarPath);
        System.out.println("Static resource mapping: /uploads/icons/** -> " + iconPath);
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .maxAge(3600);
    }
} 